import { supabase } from "@/ultils/supabase";
//signup
export async function signup(email, password, metedata = {}) {
  const { data, error } = await supabase.auth.signUp({
    email: email,
    password: password,
    options: {
      data: {
        display_name: email,
        avatar:
          "https://img.daisyui.com/images/profile/demo/yellingcat@192.webp",
        ...metedata,
      },
    },
  });
  if (error) {
    console.log(error);
    return;
  }
  return data;
}
//login
export async function login(email, password) {
  const { data, error } = await supabase.auth.signInWithPassword({
    email: email,
    password: password,
  });
  if (error) {
    console.log(error);
    throw new Error(error.message);
  }
  return data;
}
//logout
export async function logout() {
  const { error } = await supabase.auth.signOut();
  if (error) {
    console.log(error);
  }
}

//getUser
export async function getUser() {
  const {
    data: { user },
  } = await supabase.auth.getUser();
  return user;
}

//updateUser
export async function updateUser(newUserMetadata = {}) {
  const { data, error } = await supabase.auth.updateUser({
    data: newUserMetadata,
  });
  if (error) {
    console.log(error);
    return;
  }

  return data;
}
